import { getVoteList, getSearchList, showMsg } from '../../../../vuex/action'
import Configure from '../vote-configure/vote-configure.vue'
import Pages from '../../../pages/pages.vue'
export default {
  name: 'toplist',
  data() {
    return {
      voteList: [],
      showConfigure: false,
      searchType: 1,
      searchTxt: '',
      pages: {
        start: 0,
        totalPages: 0,
        limit: 20
      },
      ac: {
        customerId: '',
        votes: ''
      }
    }
  },
  components: {
    'vote-configure': Configure,
    'vue-pages': Pages
  },
  vuex: {
    actions: {
      xShowMsg: showMsg,
      xGetVoteList: getVoteList,
      xGetSearchList: getSearchList
    }
  },
  methods: {
    getList({start=this.pages.start, limit= this.pages.limit}) {
      this.xGetVoteList({
        start: start,
        limit: limit
      })
        .then((data) => {
          this.voteList = data.list
          this.pages.totalPages = Math.ceil(data.totalCount / limit)
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    searchUser({txt=this.searchTxt}) {
      let data = {}
      if (txt.length) {
        switch (+this.searchType) {
          case 1:
            data.phone = txt
            break
          case 2:
            data.uid = txt
            break
          case 3:
            data.userName = txt
            break
          default:
        }
      }
      this.xGetSearchList(data)
        .then((res) => {
          this.voteList = res.list
          this.pages.totalPages = Math.ceil(data.totalCount / 20)
        })
    },
    modifyAc(list) {
      this.showConfigure = true
      this.ac = list
    }
  },
  watch: {
    fresh() {
      if (this.fresh) {
        this.getList({})
      }
    }
  },
  ready() {
    this.getList({})
  }
}
